<template>
  <div>
    <el-tree
      ref="refPermisson"
      :data="list"
      :props="{ label: 'name' }"
      show-checkbox
      check-strictly
      default-expand-all
      node-key="id"
    />
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { arrayToTree } from '@/utils/index'
import { getRoleDetail, assignPerm } from '@/api/setting'
export default {
  props: {
    curId: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermisson()
    this.loadgetRoleDetail()
  },
  methods: {
    // 获取权限列表
    async loadPermisson() {
      const res = await getPermissionList()
      this.list = arrayToTree(res.data)
    },
    // 回填
    async loadgetRoleDetail() {
      const res = await getRoleDetail(this.curId)
      //   console.log(res)
      this.$refs.refPermisson.setCheckedKeys(res.data.permIds)
    },
    // 保存
    async hSubmit() {
      const permIds = this.$refs.refPermisson.getCheckedKeys()
      const res = await assignPerm({ id: this.curId, permIds: permIds })
      this.$message.success(res.message)
      this.$emit('success')
    },
    closeDialog() {
      this.$emit('success')
    }
  }
}
</script>

<style></style>
